package com.apexcode.rabbitextensionsdemo.controller;

import com.apexcode.rabbitextensionsdemo.constant.Constants;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ProducerController
 * @Description
 * @Author ZJX
 * @Date 2025/1/19 23:23
 * @Version 1.0
 **/
@RestController
@RequestMapping("/producer")
public class ProducerController   {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("/ack")
    public String ack(){
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,"ack","TEST ACK !");
        return "发送成功";
    }


    @RequestMapping("/confirm")
    public String confirm(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("执行confirm方法");
                if (ack){
                    System.out.println("接收到消息: "+correlationData.toString());
                } else {
                    System.out.println("未接收到"+correlationData.getId());
                    System.out.println("原因是"+cause);
                }
            }
        });

        CorrelationData correlationData = new CorrelationData("1050");
        rabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm","TEST CONFIRM",correlationData);
        return "发送成功";
    }
}
